package net.sea.setup.ui.wizard.impl.cfg.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import net.sea.commons.constants.AppConstants;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.config.custom.CustomConfigItem;
import net.sea.setup.ui.wizard.IWizardPage.PageResult;
import net.sea.setup.ui.wizard.impl.cfg.AbstractCfgPanel;

/**
 * 
 * 客户端配置页面
 *
 * @author huachengwu 
 */
public class ClientCfgPanel extends AbstractCfgPanel {
	public ClientCfgPanel() {
		create();
	}

	private static final long serialVersionUID = -5052836588447132556L;
	private JTextField clientServerIpText;
	private final JLabel clientServerPort = new JLabel(
			"\u670D\u52A1\u5668\u7AEF\u53E3\uFF1A");
	private JTextField clientServerPortText;

	@Override
	public PageResult validateInput() {
		// 验证IP
		if (!isIP(clientServerIpText.getText())) {
			return new PageResult(false, "无效的IP地址");
		}
		// 验证端口
		if (!isPort(clientServerPortText.getText())) {
			return new PageResult(false, "无效的端口号");
		}
		return new PageResult(true);
	}

	/**
	 * 创建页面
	 */
	@Override
	protected void create() {
		setBorder(new TitledBorder(null, "\u5BA2\u6237\u7AEF\u914D\u7F6E",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagLayout gbl_clientCfgPanel = new GridBagLayout();
		gbl_clientCfgPanel.columnWidths = new int[] { 10, 0, 0, 0, 0, 10, 0 };
		gbl_clientCfgPanel.rowHeights = new int[] { 10, 0, 10, 0 };
		gbl_clientCfgPanel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0,
				1.0, 0.0, Double.MIN_VALUE };
		gbl_clientCfgPanel.rowWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gbl_clientCfgPanel);

		JLabel clientServerIpLb = new JLabel("\u670D\u52A1\u5668IP\uFF1A");
		GridBagConstraints gbc_clientServerIpLb = new GridBagConstraints();
		gbc_clientServerIpLb.insets = new Insets(0, 0, 5, 5);
		gbc_clientServerIpLb.anchor = GridBagConstraints.EAST;
		gbc_clientServerIpLb.gridx = 1;
		gbc_clientServerIpLb.gridy = 1;
		add(clientServerIpLb, gbc_clientServerIpLb);

		clientServerIpText = new JTextField();
		clientServerIpText.setText("127.0.0.1");
		GridBagConstraints gbc_clientServerIpText = new GridBagConstraints();
		gbc_clientServerIpText.insets = new Insets(0, 0, 5, 5);
		gbc_clientServerIpText.fill = GridBagConstraints.HORIZONTAL;
		gbc_clientServerIpText.gridx = 2;
		gbc_clientServerIpText.gridy = 1;
		add(clientServerIpText, gbc_clientServerIpText);
		clientServerIpText.setColumns(10);
		GridBagConstraints gbc_clientServerPort = new GridBagConstraints();
		gbc_clientServerPort.anchor = GridBagConstraints.EAST;
		gbc_clientServerPort.insets = new Insets(0, 0, 5, 5);
		gbc_clientServerPort.gridx = 3;
		gbc_clientServerPort.gridy = 1;
		clientServerPort.setHorizontalAlignment(SwingConstants.TRAILING);
		add(clientServerPort, gbc_clientServerPort);

		clientServerPortText = new JTextField();
		clientServerPortText.setText("9090");
		GridBagConstraints gbc_clientServerPortText = new GridBagConstraints();
		gbc_clientServerPortText.insets = new Insets(0, 0, 5, 5);
		gbc_clientServerPortText.fill = GridBagConstraints.HORIZONTAL;
		gbc_clientServerPortText.gridx = 4;
		gbc_clientServerPortText.gridy = 1;
		add(clientServerPortText, gbc_clientServerPortText);
		clientServerPortText.setColumns(10);
	}

	@SuppressWarnings("unchecked")
	@Override
	public CustomConfig buildCfg() {
		String ipText = clientServerIpText.getText();
		String portText = clientServerPortText.getText();

		// 构建客户端自定义配置
		CustomConfig config = new CustomConfig();
		config.setType("客户端配置");
		CustomConfigItem ip = new CustomConfigItem();
		ip.setKey("服务器发布IP");
		ip.setValue(ipText);
		config.addItem(ip);

		CustomConfigItem port = new CustomConfigItem();
		port.setKey("服务器发布端口");
		port.setValue(portText);
		config.addItem(port);

		// 保存配置系统到系统上下文
		Map<String, CustomConfig> customCfgMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_MAP_KEY);
		customCfgMap.put(this.getClass().getName(), config);

		// 应用上下文配置
		String serverAddress = "dubbo://" + ipText + ":" + portText;
		contextInst.setAttribute(AppConstants.CLIENT_RELEASE_ADDRESS,
				serverAddress);
		return config;
	}
}
